Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following program of ‘C&rs... Start Learning for Free
Consider the following program of ‘C’
main ( )
{
static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
int i;
for(i = 2; I < 6; ++i)
a [a [i] ] = a [i];
for (I = 0; I < 8 ; ++i)
printf (“%d”, a[i]);
}
The output of the program is :
  • a)
    1 2 3 4 5 6 7 8
  • b)
    1 2 3 3 5 5 7 8
  • c)
    1 2 2 3 3 4 4 8
  • d)
    None of the above
Correct answer is option 'B'. Can you explain this answer?
Most Upvoted Answer
Consider the following program of ‘C’main ( ){static int a...
Array a

for(I = 2; I < 6; ++i)            // initially i = 2 and 2< 6(true)
a [a [i] ] = a [i];         
Iteration 1:
i = 2;
a[a[2]] = a[2]
a[3] = [3]
Iteration 2:
When i = 3, 3 < 6 (true)
a[a[3]] = a[3]
a[3] = 3      // as in iteration 1, a[3] becomes was 3
Iteration 3:
When i = 4
a[a[4]] = a[4]
a[5] = 5
Iteration 4:
When i = 5, 5 < 6 (true)
a[a[5]] = a[5]
a[5] = 5      // as in iteration 3 a[5] becomes 5.
In next iteration i becomes and condition 6 < 6 false here.
Content of array A are as follows:

Finally, it prints: 1 2 3 3 5 5 7 8
Free Test
Community Answer
Consider the following program of ‘C’main ( ){static int a...
Program Analysis
The given C program initializes a static array and modifies it using a loop. Let's break down the steps to understand how the output is generated.
Initialization
- The static array `a` is initialized as follows:
a = {1, 2, 3, 4, 5, 6, 7, 8}
Loop Execution
- The program contains a loop that starts with `i = 2` and runs while `i < 6`.="" the="" loop="" modifies="" the="" array="" as="" />
c
for(i = 2; i < 6;="" ++i)="" />
a[a[i]] = a[i];
}
- Iteration Details:
- i = 2:
- `a[2]` is `3`
- Update `a[3]`: `a[3] = a[2]` → `a[3] = 3` → Array: `{1, 2, 3, 3, 5, 6, 7, 8}`
- i = 3:
- `a[3]` is `3`
- Update `a[3]`: `a[3] = a[3]` (no change) → Array: `{1, 2, 3, 3, 5, 6, 7, 8}`
- i = 4:
- `a[4]` is `5`
- Update `a[5]`: `a[5] = a[4]` → `a[5] = 5` → Array: `{1, 2, 3, 3, 5, 5, 7, 8}`
- i = 5:
- `a[5]` is `5`
- Update `a[5]`: `a[5] = a[5]` (no change) → Array: `{1, 2, 3, 3, 5, 5, 7, 8}`
Final Output
- After the loop, the program prints the contents of the array:
c
for (i = 0; i < 8;="" ++i)="" />
printf("%d", a[i]);
}
- This results in the final output: `1 2 3 3 5 5 7 8`
Conclusion
- The correct answer is option 'B': `1 2 3 3 5 5 7 8`.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer?
Question Description
Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer?.
Solutions for Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer?, a detailed solution for Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? has been provided alongside types of Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following program of ‘C’main ( ){static int a [ ] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;int i;for(i = 2; I < 6; ++i)a [a [i] ] = a [i];for (I = 0; I < 8 ; ++i)printf (“%d”, a[i]);}The output of the program is :a)1 2 3 4 5 6 7 8b)1 2 3 3 5 5 7 8c)1 2 2 3 3 4 4 8d)None of the aboveCorrect answer is option 'B'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev